﻿

$(document).ready(function () {
    //默认绑定
    //debugger
    if (CPGridGlobal_GridObj.ColumnCol[0].ColumnType === 2 || CPGridGlobal_GridObj.ColumnCol[0].ColumnType === 3) {
        var FieldEnumDataIns = CPGridGlobal_GridObj.ColumnCol[0].FieldEnumDataIns;
        var FieldEnumDataSource = CPGridGlobal_GridObj.ColumnCol[0].FieldEnumDataSource;
        if (FieldEnumDataSource !== "" && FieldEnumDataSource !== null) {
            var GridCode = CPGridGlobal_GridObj.GridCode;
            var CurUserId = CPCurUserId;
            var CurUserIden = CPCurUserIden;

            var url = "/api/OrderFormCable/GetPrimarykeySource?GridCode=" + GridCode + "&CurUserId=" + CurUserId + "&CurUserIden=" + CurUserIden;
            //为了表达式，需要把URL里其它字段再加上
            //再加其它参数
            var sResultArray = CPGetQueryString();
            for (var mm = 0; mm < sResultArray.length; mm++) {
                var tempSarray = sResultArray[mm].split('=');
                if (tempSarray[0].toLowerCase() === "gridcode"
                    || tempSarray[0].toLowerCase() === "curuserid"
                    || tempSarray[0].toLowerCase() === "curuseriden"
                    || tempSarray[0].toLowerCase() === "othercondition"
                )
                    continue;
                url += "&" + sResultArray[mm];
            }
            $.getJSON(url, function (data) {
                //debugger
                if (data === "null" || data.length === 0) return;
                //绑定
                data = eval('(' + data[0] + ')');
                bindCPGridChk(data);

            });
        }
    }
    //绑定多选框
    var bindCPGridChk = function (data) {
        // debugger
        $.each(data, function (dIndex, dObj) {
            if (CPGridGlobal_GridObj.ColumnCol[0].ColumnType === 2) {

                $.each($(".CPGridChkCss"), function (nIndex, nObj) {
                    if ($(nObj).val() === dObj.id)
                        $(nObj).attr("checked", "checked");
                });
            }
            if (CPGridGlobal_GridObj.ColumnCol[0].ColumnType === 3) {
                $.each($(".CPGridRadioCss"), function (nIndex, nObj) {
                    if ($(nObj).val() === dObj.id)
                        $(nObj).attr("checked", "checked");
                });
            }
        });

    };

});

function SaveAccountDept() {
    var sSel = CPGridGetSelChkData();
    if (sSel == "") {
        alert("请选择销售部！");
        return false;
    }
    var AccounterId = $.CPGetQuery("AccounterId");
    //获取选中销售部DepId
    var DepIds = CPGridGetSelChkData();
    var inputObj = new Object();
    inputObj.CurUserId = CPCurUserId;
    inputObj.CurUserIden = CPCurUserIden;
    inputObj.AccountUserId = AccounterId;
    inputObj.DepIds = DepIds;
    //保存数据
    var url = "/api/AccountDept/SaveAccountDept";
    $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(inputObj),
        contentType: 'application/json',
        success: function (data) {
            if (data.Result === false) {
                alert("保存失败，详细信息：" + data.ErrorMsg);
                return false;
            }
            else {
                alert("保存成功！");   
                if (top.CPGridRefresh !== undefined) top.CPGridRefresh();
                //top.CloseNewModel();
            }
        }
    });
}
    

